<?php
// 本类由系统自动生成，仅供测试用途
header("Content-Type: text/html;charset=utf-8");

class CommonAction extends Action {

	public function sendsms($phone,$str)
	{
		$result = file_get_contents('http://sms.mobset.com/SDK/Sms_Send.asp?CorpID=113635&LoginName=jxt&Passwd=792949&send_no='.$phone.'&msg='.iconv('UTF-8', 'GB2312', $str));

		$resultarr =explode(',',$result);
		if ($resultarr[0] ==1)
		{
			return 1;
		}
		else
		{
			return $result;
		}


	}

	public function _initialize(){
		Load('extend');
		$this->_checkcode();
		$data['susername'] = $_SESSION['susername'];
		$data['spassword'] = $_SESSION['spassword'];
		if($_SESSION['type'] == 'teacher'){
			$data['is_teacher'] =1;
			$data['susername'] = $_SESSION['susername_goto'];
			$data['spassword'] = $_SESSION['spassword_goto'];
		}else if($_SESSION['type'] =='school'){
			$data['is_school'] = 1;
		}

		$m = M('user');
	
		$list = $m->where($data)->find();
		if($_SESSION['s_isgoto']==1){}else{
			$actionname = strtolower($this->getActionName());
			if($actionname == 'teacher' || $actionname == 'school'){
				if($actionname != $_SESSION['type']){
					$this->assign('jumpUrl',__APP__.'/Admin/login/');
					$this->assign('waitSecond','3');

					$this->error('权限不足，请联系管理员');
				}
			}
		}
		$nowmodel = MODULE_NAME;
		$nowaction = ACTION_NAME;
		$nowpath = $nowmodel.'/'.$nowaction;
		$mapp['power_url'] =$nowpath;
		$mpower =M('power');


		$powerid = getinfo($_SESSION['school_id'],'school','power');
		$pl = $mpower->where($mapp)->find();
		if (!empty($pl))
		{
			if ($mpower->where($pl['id'] .' in ('.$powerid.')')->count()==0)
			{
				$this->error('权限不足，请联系管理员');
			}
		}


		if($list == null){
			$this->assign('jumpUrl',__APP__.'/Admin/login/');
			$this->assign('waitSecond','3');
			$this->error("账号密码错误");
		}

	}



   	protected function _listpage($model, $map, $sortBy = '', $asc = false , $type = 0 , $db = '',$listRows='') {
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = $_REQUEST ['_order'];
		} else {
			$order = ! empty ( $sortBy ) ? $sortBy : $model->getPk ();
		}
		//排序方式默认按照倒序排列
		//接受 sost参数 0 表示倒序 非0都 表示正序
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		//取得满足条件的记录数
		//$keyid=$model->getpk();

		$count = $model->where ( $map )->count ();
//		echo $model->getlastsql();

		if ($count > 0) {
			import ( "ORG.Util.Page" );
			//创建分页对象
			// if (! empty ( $_REQUEST ['listRows'] )) {
			// 	$listRows = $_REQUEST ['listRows'];
			// } else {
			// 	$listRows = '';
			// }

			$p = new Page ( $count, $listRows );

			//print_r($p);
			//分页查询数据


			$voList = $model->where($map)->order( "`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );
			// echo $model ->getlastsql();
			//分页跳转的时候保证查询条件
			foreach ( $map as $key => $val ) {
				if (! is_array ( $val )) {
					$p->parameter .= "$key=" . urlencode ( $val ) . "&";
				}
			}
			//分页显示
			$page = $p->show ();
			//列表排序显示
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
			//模板赋值显示
			$this->assign ( 'list', $voList );

			//print_r($voList);
			$this->assign ( 'sort', $sort );
			$this->assign ( 'order', $order );
			$this->assign ( 'sortImg', $sortImg );
			$this->assign ( 'sortType', $sortAlt );
			$this->assign ( "page", $page );
		}

		Cookie( '_currentUrl_',$_SERVER["REQUEST_URI"]);
		return;
	}

	public function _insert($m,$data = '')//插入数据方法
	{
	    //$actionname=$this->getActionName();
	    $model=D($m);
	    if (false === $model->create ()) {
				$this->error ( $model->getError ());
			}

		if (!$data)
		{
			$isadd = $model->add($data);
		}
		else
		{
			$isadd = $model->add();
		}
	    if ($isadd)
	    {
	    //echo $model->getlastsql();
	    	return $isadd;
	    }
	    else
	    {

	    	$this->error ('新增失败!');
	    }


	}
	public function _update($m,$data = '')//插入数据方法
	{
	    //$actionname=$this->getActionName();
	    $model=D($m);
	    if (false === $model->create ()) {
				$this->error ( $model->getError ());
			}

		if (!$data)
		{
			$issave = $model->save($data);
		}
		else
		{
			$issave = $model->save();
		}
	    if ($isadd)
	    {
	    //echo $model->getlastsql();
	    	return $isadd;
	    }
	    else
	    {
	    	return 1;
	    	// $this->error ('新增失败!');
	    }


	}

	public function _create_user($type,$username,$password,$phone)
	{
		if ($type=='parent')
		{
			$data['is_parent']=1;

		}
		else if ($type=='teacher')
		{
			$data['is_teacher']=1;
		}

		if (!empty($username))
		{
			$us=$username;
		}
		else
		{
			$us =randchar();
		}

		if (!empty($password))
		{
			$ps=$password;

		}
		else
		{
			$ps =randchar(1);
		}

		$data['username'] =$us;
		$data['password'] =$ps;
		$data['password_des'] =encrypt($ps);
		$data['SJHM'] = $phone;
		$data['vcode'] = randchar();
		$data['status'] = 0;
		$data['shenhe'] = 0;
		$data['school_id'] = $_SESSION['school_id'];
		$data['register_time'] = time();
		$data['endtime'] = $data['register_time']+(3600*24*365);
		$data['addtime'] = time();
		$muser =M('user');
		$user_id = $muser->add($data);
		return $user_id;



	}

	public function loadlesaten()
	{
		$id =$_REQUEST['id'];
		$map['lesson_id'] =$id;
		$mstudent_attence =M('student_attence');
		if ($mstudent_attence->where($map)->count()>0)
		{
			echo false;
		}
		else
		{
			echo true;
		}
	}

	public function _loadlessontype()
	{
		$map['school_id']=$_SESSION['school_id'];
		$lesson_type =M('lesson_type');
            $lessonlist =$lesson_type->where('school_id='.$map['school_id'])->select();
            foreach ($lessonlist as $ltlist) {
                $time = $ltlist['starttime'];
                $timearr =explode(':',$time);
                if ($timearr[0]>12)
                {
                    $pmnum=$ltlist['num'];
                        break;
                }

                        # code...
            }

        $amlist = $lesson_type->where('school_id='.$map['school_id'].' and num<'.$pmnum)->order('num asc')->select();
        $pmlist = $lesson_type->where('school_id='.$map['school_id'].' and num>='.$pmnum)->order('num asc')->select();
        $this->assign('amlist',$amlist);
        $this->assign('pmlist',$pmlist);
	}


	public function _isempty($key,$str){
		if(empty($key)){
			$this->error($str);
		}else{
			return $key;
		}
	}


  	//上传图片
	function uploadimg($dir,$type='uniqid'){
		$upload=new UploadFile();
		$upload->maxSize=1024*20*1024;
		$upload->savePath='./uploads/'.$dir;
		$upload->saveRule= $type;
		$upload->uploadReplace=true;
		$upload->allowExts=array('jpg','jpeg','png','gif');     //准许上传的文件后缀

		$upload->allowTypes=array('image/jpeg','image/pjpeg','image/png','image/gif','image/x-png');//准许上传的文件类型
		$upload->imageClassPath = '@.ORG.Image';
		if($upload->upload())
		{
			$info=$upload->getUploadFileInfo();
			Image::thumb($info[0]['savepath'].'/'.$info[0]['savename'], $info[0]['savepath'].'/logo/osu');
			return $info;
		}else{
			$this->error($upload->getErrorMsg());
		}
	}





	private function _checkcode(){
		if($_SESSION['scode'] != null && $_SESSION['scode'] !=""){
			if($_SESSION['scode'] != C('SYSTEM_CODE')){
				$this->assign('jumpUrl',__APP__.'/Admin/login/');
				$this->assign('waitSecond','3');
				$this->error("您不能登录该系统");
	    	}
		}
	}


public function _loadids()
{
	$data =$this->_request();
        if (is_array($ids))
        {
            $ids = implode(',',$data['ids']);
        }
        else
        {
            $ids = $data['ids'];
        }
        if (empty($ids))
        {
            $this->error('请选择要删除的记录');
        }
    return $ids;
}

function class_info(){
		$id = $this->_isempty($_GET['id'],'ID不能为空');
		$m = M('class');
		$list = $m->find($id);
		$this->assign('info',$list);
		$this->display();
	}


}